package com.cz.run.bumng;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.run.beans.mysql.BumngUser;
import com.cz.run.beans.mysql.UserBase;
import com.cz.run.bumng.model.PageModel;
import com.cz.run.bumng.model.Result;
import com.cz.run.service.IBumngUserService;
import com.cz.run.service.IUserBaseService;
import com.cz.run.utils.MD5;
import com.cz.run.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by deior on 2018/6/5.
 */
@RequestMapping("/bumng")
@Controller
public class BumngController {

    @Autowired
    private IBumngUserService iBumngUserService;

    @Autowired
    private IUserBaseService iUserBaseService;

    @RequestMapping(value = "/login.htm",method = RequestMethod.GET)
    public String login(Model model){
        return "login";
    }

    @RequestMapping(value ="/login.do",method = RequestMethod.POST)
    @ResponseBody
    public Object login(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){
        if(null==bumngUser){

            return  new Result(false,"请输入账号密码");
        }

        if(null==bumngUser.getName()||"".equals(bumngUser.getPassword())){

            return  new Result(false,"请输入账号密码");
        }

        EntityWrapper<BumngUser> ew = new EntityWrapper<>();
        ew.where("name={0}",bumngUser.getName());
        BumngUser bumngUser1 = iBumngUserService.selectOne(ew);

        if(null==bumngUser1){

            return  new Result(false,"用户不存在，请确认账号!");
        }

        if(!MD5.GetMD5Code(bumngUser.getPassword()).equals(bumngUser1.getPassword())){

            return  new Result(false,"密码错误,请重新输入!");
        }
        model.addAttribute("bumngUser",bumngUser);

        request.getSession().setAttribute("bumngUser",bumngUser);

        return  new Result(true,"成功");

    }
    @RequestMapping(value ="/index.htm",method = RequestMethod.GET)
    public String index(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){

        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);

        String phone = request.getParameter("phone");
        model.addAttribute("phone",phone);

        PageModel pageModel =  PageUtil.getPageData(request);

        EntityWrapper<UserBase> ew = new EntityWrapper<>();
        ew.where("1 = 1").and(StringUtils.hasLength(phone),"phone = {0}",phone).
                orderBy("create_time desc limit "+pageModel.getPageNum()+","+pageModel.getPageSize());
        List userList =  iUserBaseService.selectList(ew);

        EntityWrapper<UserBase> ew1 = new EntityWrapper<>();
        ew1.where(StringUtils.hasLength(phone),"phone = {0}",phone);
        int count =  iUserBaseService.selectCount(ew1);


        model.addAttribute("pageTotal",count%pageModel.getPageSize()>0?count/10+1:count/10);
        model.addAttribute("pageNum",(pageModel.getPageNum())/10+1);

        model.addAttribute("userList",userList);



        return "index";
    }

}
